
let audio = new Audio();
import { Toast } from 'mint-ui';

// 接收音乐的id并播放
audio.playSong = function (id) {
	this.vm.$store.dispatch('getSongdetails',id)
	this.src = `https://music.163.com/song/media/outer/url?id=${id}.mp3`;
	this.play();
	//检查音乐接口
	let checkurl = {
		url: '/check/music',
		params: {
			id: id
		}
	};
	// let songdetail = {
	// 	url: '/song/detail',
	// 	method: 'get',
	// 	//此处传入歌曲ids
	// 	params: {
	// 		ids: id
	// 	}
	// };
	// this.vm.getsource(songdetail).then(res => {
	// 	console.log('songdetail', res)
	// 	this.vm.$store.commit('songInfoDetails', res.data.songs[0])
	// })
	//检查音乐是否可用,不可用则自动播放下一首
	this.vm.getsource(checkurl).then(res => {
		if (res.data.success == false) {
			Toast({
				message: "暂无版权",
			});
			setTimeout(() => {
				this.vm.$store.commit("changeIndex", 'next');
				this.playSong(this.vm.$store.getters.playdetails.id)
			}, 3000);
		} 
	});

}

//监听是否加载完毕
audio.addEventListener('canplay', function () {
	// console.log('加载好了');
	let duration = this.duration;
	this.vm.$store.commit("duration", duration);
})
//监听是否播放
audio.addEventListener('play', function () {
	this.vm.$store.commit("audioState", true);
})
//监听是否暂停
audio.addEventListener('pause', function () {
	this.vm.$store.commit("audioState", false);
})
//监听播放时间变化
audio.addEventListener('timeupdate', function () {
	this.vm.$store.commit("currenttime", this.currentTime);
	// this.vm.$store.commit("currenttime", 18);
})
//歌曲加载错误
audio.addEventListener('error', function () {

})
//监听歌曲播放结束
audio.addEventListener('ended', function () {
	this.vm.$store.commit("changeIndex", 'next');
	this.playSong(this.vm.$store.getters.playdetails.id)
})
export default audio 